package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import app.Disthanitian.thaistyle.EmerMez.AddUserLog.WebPageTask;

public class AddRiskZone {

	static UrlEmerMez URLlink = new UrlEmerMez();
	public static final String urlAdd = URLlink.getUrladdriskzone();
	public static final String urlEdit = URLlink.getUrleditriskzone();
	public static final String urlDelete = URLlink.getUrldeleteriskzone();
	public static final String urlSelect = URLlink.getUrldeleteriskzone();
	public static final String urlAddRiskZoneOption = URLlink.getUrladdriskzoneoption();
	public static String result = "";
	
	MemberUser user = new MemberUser();
	 String Useremail ;
	 String UserID; // UserID
	 String password;
	 String firstname;
	 String lastname;
	 String phoneNum;
	 int timeLocateUpdate;
	 String pinCode;
	 String msgRed;
	 String msgYellow;
	 String confirmation;
	 String type;
	 
	 //RiskZone data = new RiskZone();
	 
		String id;
		String nameStart;
		String nameEnd;
	    String oiriginLat;
		String originLng;
	    String destinationLat;
		String destinationLng;
		String Risktype;
		String time;
		String date;
		DatabaseConnection db;
		SQLiteDatabase mdb;
		Cursor mCursor;
		String riskId;
		String enable;
		
		
		
		public void callRun(){
			
			WebPageTask task = new WebPageTask();
			task.execute();
		}
		
		
		
		public void setVariable(RiskZone data){
			UserID = data.getUserId(); // UserID 
			
			nameStart=data.getNameStart();
			nameEnd = data.getNameEnd();
		    oiriginLat = data.getOiriginLat();
			originLng = data.getOriginLng();
		    destinationLat = data.getDestinationLat();
			destinationLng= data.getDestinationLng();
			type = data.getType();
			time = data.getTime();
			date = data.getDate();

		}
		public String DataRiskZone() {
		   
			
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("userId", UserID ));
			nameValuePairs.add(new BasicNameValuePair(" nameStart",  nameStart));
			nameValuePairs.add(new BasicNameValuePair(" nameEnd",  nameEnd));
			nameValuePairs.add(new BasicNameValuePair(" oiriginLat", oiriginLat));
			nameValuePairs.add(new BasicNameValuePair(" originLng", originLng));
			nameValuePairs.add(new BasicNameValuePair(" destinationLat",  destinationLat));
			nameValuePairs.add(new BasicNameValuePair(" destinationLng",destinationLng));
			nameValuePairs.add(new BasicNameValuePair(" type", type));
			nameValuePairs.add(new BasicNameValuePair(" date", date));
			nameValuePairs.add(new BasicNameValuePair(" time", time));

			return postData(urlAdd, nameValuePairs);

		}

		public String postData(String url, List<NameValuePair> nameValuePairs) {

			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);
			BufferedReader in = null;
			StringBuffer sb = new StringBuffer("");
			try {
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				HttpResponse response = httpclient.execute(httppost);
				in = new BufferedReader(new InputStreamReader(response.getEntity()
						.getContent()));

				String line = "";
				String NL = System.getProperty("line.separator");
				while ((line = in.readLine()) != null) {
					sb.append(line + NL);
				}
				in.close();
				String page = sb.toString();
				return page;
			} catch (ClientProtocolException e) {
			} catch (IOException e) {
			}
			return null;
		}

		protected class WebPageTask extends AsyncTask<Void, Void, String> {

			@Override
			protected String doInBackground(Void... params) {
			
				result =DataRiskZone();
				return result;
			}

			protected void onPostExecute(String result) {
				
				//Toast.makeText(RegisterActivity.this,result + "Hi",Toast.LENGTH_LONG).show();
				
				if (result.trim().equals("ok")) {
					
					//Toast.makeText(AddUserLog.this,"Sign up success!",Toast.LENGTH_LONG).show();

				
				} 

			}
		
	            
	}
		
		public void callRunInsertRiskZoneOption(){
			
			AddRiskZoneOption task = new AddRiskZoneOption();
			task.execute();
		}
		
		
		
		public void setVariable2(String userId,String riskId,String enable){
			Date currentTime = new Date();
			final SimpleDateFormat formatDate =
	                new SimpleDateFormat("yyyy-MM-dd");
	         final SimpleDateFormat formatTime =
	                new SimpleDateFormat("HH:mm:ss");  
			UserID = userId; // UserID 
			this.riskId = riskId;			
			time = formatTime.format(currentTime).toString();
			date = formatDate.format(currentTime).toString();
			this.enable = enable;
		}
		public String DataRiskZoneOption() {
		   
			
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("riskId", riskId ));
			nameValuePairs.add(new BasicNameValuePair("userId", UserID));
			nameValuePairs.add(new BasicNameValuePair("dateRate", date));
			nameValuePairs.add(new BasicNameValuePair("timeRate", time));
			nameValuePairs.add(new BasicNameValuePair("enable", enable));	

			return postData1(urlAddRiskZoneOption, nameValuePairs);

		}

		public String postData1(String url, List<NameValuePair> nameValuePairs) {

			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);
			BufferedReader in = null;
			StringBuffer sb = new StringBuffer("");
			try {
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				HttpResponse response = httpclient.execute(httppost);
				in = new BufferedReader(new InputStreamReader(response.getEntity()
						.getContent()));

				String line = "";
				String NL = System.getProperty("line.separator");
				while ((line = in.readLine()) != null) {
					sb.append(line + NL);
				}
				in.close();
				String page = sb.toString();
				return page;
			} catch (ClientProtocolException e) {
			} catch (IOException e) {
			}
			return null;
		}

		protected class AddRiskZoneOption extends AsyncTask<Void, Void, String> {

			@Override
			protected String doInBackground(Void... params) {
			
				result =DataRiskZoneOption();
				return result;
			}

			protected void onPostExecute(String result) {
				
				//Toast.makeText(RegisterActivity.this,result + "Hi",Toast.LENGTH_LONG).show();
				
				if (result.trim().equals("ok")) {
					
					//Toast.makeText(AddUserLog.this,"Sign up success!",Toast.LENGTH_LONG).show();

				
				} 

			}
		
	            
	}
		
		//EDIT Riskzone --> name,lat,lng --------------
		
		/*public String EditRiskZone() {			

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("id", id));
			nameValuePairs.add(new BasicNameValuePair(" name",  name));
			nameValuePairs.add(new BasicNameValuePair(" oiriginLat", oiriginLat));
			nameValuePairs.add(new BasicNameValuePair(" originLng", originLng));
			nameValuePairs.add(new BasicNameValuePair("  destinationLat",  destinationLat));
			nameValuePairs.add(new BasicNameValuePair("destinationLng",destinationLng));
			nameValuePairs.add(new BasicNameValuePair(" Risktype", Risktype));
			nameValuePairs.add(new BasicNameValuePair(" time", time));

			return postDataEdit(urlEdit, nameValuePairs);

		}*/

		public String postDataEdit(String url, List<NameValuePair> nameValuePairs) {

			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);
			BufferedReader in = null;
			StringBuffer sb = new StringBuffer("");
			try {
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				HttpResponse response = httpclient.execute(httppost);
				in = new BufferedReader(new InputStreamReader(response.getEntity()
						.getContent()));

				String line = "";
				String NL = System.getProperty("line.separator");
				while ((line = in.readLine()) != null) {
					sb.append(line + NL);
				}
				in.close();
				String page = sb.toString();
				return page;
			} catch (ClientProtocolException e) {
			} catch (IOException e) {
			}
			return null;
		}

		/*protected class RunEditRiskZone extends AsyncTask<Void, Void, String> {

			@Override
			protected String doInBackground(Void... params) {
			
				result =EditRiskZone();
				return result;
			}

			protected void onPostExecute(String result) {
							
				
				if (result.trim().equals("ok")) {
					
					//Toast.makeText(AddUserLog.this,"Sign up success!",Toast.LENGTH_LONG).show();

				
				} 

			}
		
	            
	}*/
		//-----------------------END EDIT---------------------
	
}
